﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.Form
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;

    public partial class FrmThongke_Hangban : SMP.Windows.Forms.FrmBase
    {
        Thongke_Service _objThongke = new Thongke_Service();
        DataTable dtb_thongke = new DataTable();

        public FrmThongke_Hangban()
        {
            InitializeComponent();

            item_Add.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Edit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Delete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Save.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Cancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            item_Report.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
        }


        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {
                this.FormState = Windows.Forms.FormState.View;
                ResetText();
                dt_Ngaykt.EditValue = DateTime.Now;
            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ResetText()
        {
            dt_Ngaybd.EditValue = null;
            dt_Ngaykt.EditValue = null;

            dt_Ngaybd.Focus();
        }

        #endregion


        public override bool PerformReport()
        {
            Hashtable hashtableControl = new Hashtable();
            hashtableControl.Add(dt_Ngaybd, lbNgaybd);
            hashtableControl.Add(dt_Ngaykt, lbNgaykt);
            if (!Windows.Utility.Validator.CheckNullFields(hashtableControl))
                return false;

            if (dt_Ngaybd.DateTime.Date > dt_Ngaykt.DateTime.Date)
            {
                Windows.Forms.Message.Show("Ngày bắt đầu thống kê phải nhỏ hơn ngày kết thúc", "Thông Báo");
                return false;
            }

            dtb_thongke = _objThongke.Get_Thongke_Hangban_Theongay(dt_Ngaybd.DateTime.Date, dt_Ngaykt.DateTime.Date);
            dgMathang.DataSource = dtb_thongke;

            return true;
        }

        public override bool PerformPrintPreview()
        {
            if (gvMathang.RowCount == 0)
            {
                Windows.Forms.Message.Show("Không có mặt hàng để IN", "Thông Báo");
                return false;
            }

            var frm = new Reports.FrmRpt();
            frm.Text = "BẢNG KÊ CHI TIẾT HÀNG BÁN";
            var rpt = new Reports.XR_Hangban_Theongay();
            Dataset.dsThongke_Hangban _dsHangban_Theongay = new Dataset.dsThongke_Hangban();
            
            //fill data report
            rpt.DataSource = _dsHangban_Theongay;
            foreach (DataRow dr in dtb_thongke.Rows)
            {
                var newRow = _dsHangban_Theongay.Tables["dtHangban_Theongay"].NewRow();
                foreach (DataColumn col in dtb_thongke.Columns)
                {
                    try { newRow[col.ColumnName] = dr[col.ColumnName]; }
                    catch { continue; }
                }
                _dsHangban_Theongay.Tables["dtHangban_Theongay"].Rows.Add(newRow);
            }
            frm.SetReport(rpt);
            frm.Show();
            frm.Activate();

            return true;
        }

        private void gvMathang_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }








    }
}